//
// Created by v on 2019/10/23 下午5:47.
// Copyright (c) 2019 jmdvirus All rights reserved.
//
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//    * //   *  *  *   *\ / ||  |  |    |
//     **    *  *  *---* *  *   *--* *--*
//

#include "CMLanguageConfig.h"

CMLanguage::CMLanguage(const char *filename) { m_filename = filename; }

int CMLanguage::load() { return cm_config_read_ex(m_filename.c_str(), m_lang); }

const std::string &CMLanguage::getLang(const char *key) { return m_lang[key]; }

CMLanguageConfig::CMLanguageConfig() {}

CMLanguageConfig::~CMLanguageConfig()
{
    std::map<std::string, CMLanguage *>::iterator iter = m_langs.begin();
    for (; iter != m_langs.end();)
    {
        CMLanguage *tmp = iter->second;
        iter            = m_langs.erase(iter);
        delete tmp;
    }
}

int CMLanguageConfig::load()
{
    CMLanguage *lang = new CMLanguage("/etc/language_zh.ini");
    lang->load();
    m_langs["zh"] = lang;

    CMLanguage *lang_en = new CMLanguage("/etc/language_en.ini");
    lang->load();
    m_langs["en"] = lang_en;
    return 0;
}

const std::string &CMLanguageConfig::getLangZh(const char *key)
{
    return getLang("zh", key);
}

const std::string &CMLanguageConfig::getLangEn(const char *key)
{
    return getLang("en", key);
}

const std::string &CMLanguageConfig::getLang(const char *lang, const char *key)
{
    return m_langs[lang]->getLang(key);
}
